package com.sunwayhorizo.health.business.registries.threelevels.query;

import java.util.List;

import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import org.jboss.seam.annotations.Name;

import com.sunwayhorizo.health.common.ListUtil;
import com.sunwayhorizo.health.common.StrUtil;
import com.sunwayhorizo.health.hl7.common.Hl7Exception;
import com.sunwayhorizo.health.hl7.rim.ActParticipation;
import com.sunwayhorizo.health.hl7.rim.Role;
import com.sunwayhorizo.health.hl7.vocabulary.ActCode;
import com.sunwayhorizo.health.hl7.vocabulary.RoleCode;

@Name("ThreeLevelQuery")
@Stateless
public class ThreeLevelQuery implements ThreeLevelQueryLocal
{
    @PersistenceContext(unitName = "sunwayhealth")
     private EntityManager em;
    
    @SuppressWarnings({ "unchecked"})
    public List<ActParticipation> findActPrtcptnsByAct(Long roleId,String typeCode) throws Hl7Exception
    {
        StringBuilder jpql = new StringBuilder();
        jpql.append(" select ap from ActParticipation ap ");
        jpql.append(" where ap.participationAct.actId= ");
        jpql.append(" ( select p.participationAct.actId from ActParticipation p where p.typeCode='"+typeCode+"' and p.roleParticipation.roleId="+roleId+")");
        List<ActParticipation> parList = em.createQuery(jpql.toString()).getResultList();
        return parList;
    }
    
    @SuppressWarnings({ "unchecked"})
    public List<ActParticipation> findActPrtcptnsByRole(Long roleId,String typeCode) throws Hl7Exception
    {
        StringBuilder jpql = new StringBuilder();
        jpql.append("  select p from ActParticipation p where p.typeCode='"+typeCode+"' and p.roleParticipation.roleId="+roleId+"");
        List<ActParticipation> parList = em.createQuery(jpql.toString()).getResultList();
        return parList;
    }
    
    @SuppressWarnings({ "unchecked"})
    public ActParticipation findActPrtcptnsByRoleId(Long roleId,String typeCode) throws Hl7Exception
    {
        StringBuilder jpql = new StringBuilder();
        jpql.append("  select p from ActParticipation p where p.typeCode='"+typeCode+"' and p.roleParticipation.roleId="+roleId+"");
        List<ActParticipation> parList = em.createQuery(jpql.toString()).getResultList();
        return (ListUtil.isNotEmpty(parList) ? parList.get(0) : null);
    }
    
    @SuppressWarnings("unchecked")
    public List<ActParticipation> findThreeLevelListByRole(Role role) throws Hl7Exception
    {
        if(null == role) return null;
        StringBuilder jbql = new StringBuilder();
        jbql.append(" select act.actId from Act act ");
        jbql.append(" left join act.prtcptnsActList part ");
        jbql.append(" where act.codeEtsId='"+ActCode.MEDICALTEAM.code().toString()+"' ");
        jbql.append(" and part.roleParticipation.roleId="+role.getRoleId()+"");
        List<Long> actIdList = em.createQuery(jbql.toString()).getResultList();
        long actid = 0;
        if(ListUtil.isNotEmpty(actIdList))
        {
            actid = actIdList.get(0);
        }
        StringBuilder jpql = new StringBuilder();
        jpql.append(" select p from ActParticipation p where p.participationAct.actId="+actid+"");
        List<ActParticipation> list = em.createQuery(jpql.toString()).getResultList();
        return list;
    }
    
    @SuppressWarnings("unchecked")
    public  List<Role> findThreeLevelListByWorkGroupId(String workgroupId) throws Hl7Exception
    {
        StringBuilder sb = new StringBuilder();
        sb.append(" select r from Role r where r.codeEtsId='"+ RoleCode.MEDICALTEAM.toString() + "' ");
        if (StrUtil.isNotEmpty(workgroupId)) 
        {
            sb.append(" and r.scoperEntities.entityId=" + workgroupId);
        }
        List<Role> list = em.createQuery(sb.toString()).getResultList();
        return list;
    }
    
    
}
